import Taro from "@tarojs/taro";
export default class SockJSClient {
  constructor(url) {
    this.socket = null;
    this.url = url;
  }

  connect() {
    this.socket = Taro.connectSocket({
      url: this.url,
    });

    this.socket.onOpen(() => {
      console.log("连接成功");
    });

    this.socket.onMessage((res) => {
      console.log("接收到消息:", res.data);
    });

    this.socket.onClose(() => {
      console.log("连接关闭");
    });

    this.socket.onError((error) => {
      console.error("连接错误:", error);
    });
  }

  send(message) {
    if (this.socket) {
      this.socket.send({
        data: message,
      });
    }
  }

  close() {
    if (this.socket) {
      this.socket.close();
    }
  }
}
